package com.hdjk.view.activity.main;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.network.Network;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.ValidateUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by sxy on 2017/11/3.
 */

/**
 * 注册页
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private EditText et_phone;
    private EditText et_code;
    private TextView tv_send_code;
    private EditText et_pwd;
    private EditText et_recommend;
    private Button btn_register;
    // End Of Content View Elements

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_code = (EditText) findViewById(R.id.et_code);
        tv_send_code = (TextView) findViewById(R.id.tv_send_code);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        et_recommend = (EditText) findViewById(R.id.et_recommend);
        btn_register = (Button) findViewById(R.id.btn_register);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        tv_send_code.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.tv_send_code:
                getPhoneCode();
                break;
            case R.id.btn_register:
                register();
                break;
        }
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tv_send_code.setText("已发送" + (millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            tv_send_code.setEnabled(true);
            tv_send_code.setBackgroundResource(R.drawable.button_yanzhengma_sel);
            tv_send_code.setTextColor(getResources().getColor(R.color.white));
            tv_send_code.setText("获取验证码");
        }
    };

    private void getPhoneCode() {
        String phone = et_phone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShortToast(this, "请输入手机号");
            return;
        }
        mDialog.show();
        JSONObject obj = new JSONObject();
        try {
            obj.put("Phone", phone);
            obj.put("Type", 0);
            obj.put("CheckExistentOrNo",false);
            Network.getPhoneCode(obj.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    mDialog.cancel();
                    if (msg != null && msg.getSuccess()) {
                        tv_send_code.setBackgroundResource(R.drawable.utton_yanzhengma_dis);
                        tv_send_code.setTextColor(getResources().getColor(R.color.gray_aaaaaaa));
                        tv_send_code.setEnabled(false);
                        timer.start();
                        ToastUtils.showShortToast(RegisterActivity.this, "验证码发送成功");
                    } else {
                        if (msg != null && !TextUtils.isEmpty(msg.getMsg()) && !"null".equals(msg.getMsg())) {
                            ToastUtils.showShortToast(RegisterActivity.this, msg.getMsg());
                        } else {
                            ToastUtils.showShortToast(RegisterActivity.this, "验证码发送失败");
                        }
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void register() {
        String phone = et_phone.getText().toString();
        String code = et_code.getText().toString();
        String pwd = et_pwd.getText().toString();
        String recommend = et_recommend.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showShortToast(this, "请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtils.showShortToast(this, "请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtils.showShortToast(this, "请输入密码");
            return;
        }
        if (!ValidateUtil.isMobileNO(phone)) {
            ToastUtils.showShortToast(this, "请输入正确的手机号码");
            return;
        }
        mDialog.show();
        JSONObject obj = new JSONObject();
        try {
            obj.put("Phone", phone);
            obj.put("Pwd", pwd);
            if (!TextUtils.isEmpty(recommend)) {
                obj.put("RefereeId", recommend);
            }
            obj.put("PhoneCheckCode", code);
            Network.register(obj.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    mDialog.cancel();
                    if (msg != null && msg.getSuccess()) {
                        ToastUtils.showShortToast(RegisterActivity.this, "注册成功");
                        back();
                    } else {
                        if (msg != null && !TextUtils.isEmpty(msg.getMsg()) && !"null".equals(msg.getMsg())) {
                            ToastUtils.showShortToast(RegisterActivity.this, msg.getMsg());
                        } else {
                            ToastUtils.showShortToast(RegisterActivity.this, "注册失败");
                        }
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
